package com.javalong.affairs.activity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.common.ServiceLocator;
import com.javalong.affairs.common.UIHandler;
import com.javalong.affairs.constant.BundleKey;
import com.javalong.affairs.constant.RequestCode;
import com.javalong.affairs.constant.RequestKey;
import com.javalong.affairs.data.AllData;
import com.javalong.affairs.listener.BaseResponseListener;
import com.javalong.affairs.model.Dept;
import com.javalong.affairs.model.User;
import com.javalong.affairs.ui.TextSelectView;
import com.javalong.affairs.ui.TopTitle;
import com.javalong.affairs.utils.HttpUtils;
import com.javalong.affairs.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_updateuser)
public class UpdateUserInfoActivity extends BaseActivity {

	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.et_username)
	private EditText et_username;
	@ViewInject(R.id.et_workid)
	private EditText et_workid;
	@ViewInject(R.id.et_phone)
	private EditText et_phone;
	@ViewInject(R.id.ui_tsview_dept)
	private TextSelectView tsView_dept;
	@ViewInject(R.id.bt_update)
	private Button bt_update;
	private User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		initData();
		initUI();
	}

	private void initUI() {
		bt_update.setOnClickListener(this);
		topTitle.setOnLeftDrawableClick(this);

		et_phone.setText(user.phone);
		et_username.setText(user.name);
		et_workid.setText(user.workId);
	}

	private void initData() {
		user = (User) AllData.GetInstance()
				.get(R.string.data_user_current_user);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.bt_update:
			user.workId=et_workid.getText().toString();
			user.name = et_username.getText().toString();
			user.phone = et_phone.getText().toString();
			String url = UrlUtils.getUrl("User", "setDetail");
			Map<String, Object> params = new HashMap<String, Object>();
			params.put(RequestKey.WORKID, et_workid.getText().toString());
			params.put(RequestKey.NAME, et_username.getText().toString());
			params.put(RequestKey.PHONE, et_phone.getText().toString());
			progressDialog.show();
			HttpUtils.post(url, params, new BaseResponseListener(
					new TypeToken<User>() {
					}.getType()) {
				@Override
				public void onSuccess(Object obj) {
					super.onSuccess(obj);
					progressDialog.dismiss();
					finish();
				}
			});
			break;
		default:
			break;
		}
	}

}
